package com.starrtc.demo.imclient.clientHandler;

import com.starrtc.demo.imclient.client.ClientSession;
import com.starrtc.demo.imclient.client.Controller;
import com.starrtc.demo.imclient.iMcommon.ProtoInstant;
import com.starrtc.demo.imclient.iMcommon.bean.msg.ProtoMsg;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

@ChannelHandler.Sharable
public class LoginResponceHandler extends ChannelInboundHandlerAdapter {

    public static LoginResponceHandler instance;

    public static LoginResponceHandler getInstance(){
        if(instance == null){
            instance = new LoginResponceHandler();
            return instance;
        }
        return instance;
    }

    private LoginResponceHandler(){

    }

    /**
     * 业务逻辑处理
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        //判断消息实例
        if (null == msg || !(msg instanceof ProtoMsg.Message)) {
            super.channelRead(ctx, msg);
            return;
        }

        //判断类型
        ProtoMsg.Message pkg = (ProtoMsg.Message) msg;
        ProtoMsg.HeadType headType = ((ProtoMsg.Message) msg).getType();
        if (!headType.equals(ProtoMsg.HeadType.LOGIN_RESPONSE)) {
            super.channelRead(ctx, msg);
            return;
        }


        //判断返回是否成功
        ProtoMsg.LoginResponse info = pkg.getLoginResponse();

        ProtoInstant.ResultCodeEnum result =
                ProtoInstant.ResultCodeEnum.values()[info.getCode()];

        if (!result.equals(ProtoInstant.ResultCodeEnum.SUCCESS)) {
//            log.info(result.getDesc());
//            log.info("step3：登录Netty 服务节点失败");
        } else {

            ClientSession session =
                    ctx.channel().attr(ClientSession.SESSION_KEY).get();
            session.setSessionId(pkg.getSessionId());
            session.setLogin(true);

//            log.info("step3：登录Netty 服务节点成功");
            Controller.getInstance().notifyCommandThread();

            ctx.channel().pipeline().addAfter("loginResponseHandler","heartBeatClientHandler",HeartBeatClientHandler.getInstance());
            ctx.channel().pipeline().remove("loginResponseHandler");

        }


    }


}
